package com.example.service.impl;

import com.example.service.RedisService;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * ClassName:RedisServiceImpl
 * Package:com.example.service.impl
 * User:HP
 * Date:2025/8/30
 * Time:上午12:35
 * Author 周东汉
 * Version 1.0
 * Description:
 */

@Service
public class RedisServiceImpl implements RedisService {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void borrowBook(Integer userId, Integer bookId, Integer days) {
        StringBuilder sb = new StringBuilder();
        sb.append("borrowBook:").append(userId).append(":").append(bookId);
        String key = sb.toString();
        redisTemplate.opsForValue().set(key, "borrowed", days, TimeUnit.DAYS);
    }

    @Override
    public void returnBook(Integer userId, Integer bookId) {
        StringBuilder sb = new StringBuilder();
        sb.append("borrowBook:").append(userId).append(":").append(bookId);
        String key = sb.toString();
        redisTemplate.delete(key);
    }
}
